﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fApp.Forms
{
    public partial class fTextBox : TextBox
    {


        private bool _isRule = true;
        public bool isRule
        {
            get { return _isRule; }
            set { _isRule = value; Invalidate(); }
        }
        public fTextBox()
        {
            this.BorderStyle = BorderStyle.None;
            this.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.Multiline = true;
        }


        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (isRule)
            {

                Graphics g = this.CreateGraphics();
                StringFormat sf = new StringFormat();
                int lineWidth = this.ClientSize.Width - 3;

                CharacterRange[] characterRanges = { new CharacterRange(0, 1) };
                sf.SetMeasurableCharacterRanges(characterRanges);
                RectangleF layoutRect = new RectangleF(0, 0, 100, 100);
                Region[] stringRegions = g.MeasureCharacterRanges("|", this.Font, layoutRect, sf);
                RectangleF charRect = stringRegions[0].GetBounds(g);
                int lineHeight = (int)charRect.Height;

                Pen pen = new Pen(Color.LightGray);
                int clientHeight = this.ClientSize.Height;
                for (int i = 1; i * lineHeight < clientHeight; i++)
                {
                    g.DrawLine(pen, new Point(2, lineHeight * i), new Point(lineWidth, lineHeight * i));
                }
                pen.Dispose();
            }
        }
    }
}
